<?php
/**
 * This is the template for generating a controller class file for CRUD feature.
 * The following variables are available in this template:
 * - $this: the BootCrudCode object
 */
?>
<?php echo "<?php\n"; ?>

class <?php echo $this->controllerClass; ?> extends <?php echo $this->baseControllerClass . "\n"; ?>
{
    public $layout='//layouts/column2';

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index','view','read'),
                'users'=>array('*'),
            ),
            array('allow',
                'users'=>array('admin'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }

    protected function getIndexList($model) {
        $indexList = array(
            <?php
            $ignores = array('created_at','modified_at','created_by','modified_by');
            foreach ($this->tableSchema->columns as $column) {
                $name_ = explode('_', $column->name, 2);
                $name = isset($name_[1]) ? $name_[1] : $column->name;    
                if ($column->isPrimaryKey || in_array($name, $ignores) || in_array($column->name, $ignores)) {
                    continue;
                } else {
                    echo "array(\n";
                    echo "\t\t'name' => '$column->name',\n";
                    if ($column->allowNull) {
                        echo "\t\t'visible' => false,\n";
                    }
                    if ($column->isForeignKey) {
                        $rel = $this->getRelatedname($this->tableSchema->foreignKeys[$column->name][0]);
                        echo "\t\t'value' => '(\$data->{$rel[0]}) ? \$data->{$rel[0]}->{$rel[1]} : \"-\"',\n";
                    } else {
                        echo "\t\t'value' => '(\$data->{$column->name}) ? \$data->{$column->name} : \"-\"',\n";
                    }
                    echo "\t),";
                }
            }
            ?>
            array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn'
            ),
        );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            <?php
            foreach ($this->tableSchema->columns as $column) {
                $name_ = explode('_', $column->name, 2);
                $name = isset($name_[1]) ? $name_[1] : $column->name;    
                if ($column->isPrimaryKey || in_array($name, $ignores) || in_array($column->name, $ignores)) {
                    continue;
                } else {
                    echo "array(\n";
                    echo "\t'name' => '$column->name',\n";
                    if ($column->isForeignKey) {
                        $rel = $this->getRelatedname($this->tableSchema->foreignKeys[$column->name][0]);
                        echo "\t'value' => (\$model->{$rel[0]}) ? \$model->{$rel[0]}->{$rel[1]} : \"-\",\n";
                    } else {
                        echo "\t'value' => (\$model->{$column->name}) ? \$model->{$column->name} : \"-\",\n";
                    }
                    echo "),";
                }
            }
            ?>
        );
        return $viewList;
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new <?php echo $this->modelClass; ?>;
        $this->performAjaxValidation($model);
        if(isset($_POST['<?php echo $this->modelClass; ?>']))
        {
            $model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['<?php echo $this->modelClass; ?>']))
        {
            $model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new <?php echo $this->modelClass; ?>('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['<?php echo $this->modelClass; ?>']))
            $model->attributes=$_GET['<?php echo $this->modelClass; ?>'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new <?php echo $this->modelClass; ?>('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['<?php echo $this->modelClass; ?>']))
            $model->attributes=$_GET['<?php echo $this->modelClass; ?>'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=<?php echo $this->modelClass; ?>::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($this->modelClass); ?>-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
